package service;

import common.util.PageBean;
import mapper.SysUsersMapper;
import model.req.SysUserReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import model.dto.SysUserDto;

import java.util.List;

/**
 * @className: service.SysUserServiceImpl
 * @author: Xia
 * @create: 2023-03-09 16:14
 */
@Service
public class SysUsersServiceImpl implements SysUsersService{
    @Autowired
    private SysUsersMapper mapper;

    @Override
    public SysUserDto getSysUser(String account , String password) {
        SysUserDto sysUser = mapper.getSysUser(account);
        if (sysUser == null){
            return null;
        }
        if (!sysUser.getPassword().equals(password)){
            return null;
        }
        return sysUser;
    }

//    @Override
//    public List<SysUserDto> getUsersList(SysUserReq sysUserReq) {
//        return mapper.getUsersList(sysUserReq);
//    }

    @Override
    public Integer addUser(SysUserReq sysUserReq) {
        return mapper.addUser(sysUserReq);
    }

    @Override
    public SysUserDto getUserById(Long id) {
        return mapper.getUserById(id);
    }

    @Override
    public PageBean<SysUserDto> page(Integer userType, String account, String userName, Integer index) {
        PageBean<SysUserDto> pageBean = new PageBean<>();
//            1.获取数据总条数
        Integer usersCount = mapper.getUsersCount();
        pageBean.setTotal(usersCount);
//            2.获取总页数
        Integer totalPage = usersCount % pageBean.pageSize == 0 ? usersCount / pageBean.pageSize : usersCount / pageBean.pageSize+1;
//            3.判断如果首页数大于总页数的话，首页数=总页数；
        if(index>totalPage){
            index=totalPage;
        }
//            4.判断如果首页数小于1的话，首页数=1
        if(index<1){
            index=1;
        }
        Integer start = (index-1)*pageBean.pageSize;
        List<SysUserDto> users = mapper.getUsersList(userType,account,userName, start,pageBean.pageSize);
        pageBean.setPages(totalPage);
        pageBean.setData(users);
        pageBean.setPageNo(index);
        return pageBean;
    }

    @Override
    public List<SysUserDto> getDoctorList(Integer departmentId, String userName) {
        return mapper.getDoctorList(departmentId, userName);
    }
}
